using UnityEngine;
using System.Collections;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif

public class StartMenu : MonoBehaviour 
{
	public Color color1 = Color.red;
	public Color color2 = Color.blue;
	public Color color3 = Color.green;
	public float duration = 3.0f;
	public float t;
	public Text fovText, sfxText, musicText;
	public Canvas mainMenu, optionsMenu, soundMenu, videoMenu, loadMenu;
	public Slider fovSlider, sfxSlider, musicSlider;
	public int fovValue;
	public float sfxValue, musicValue;

	void Start ()
	{
		SetDefPref ();
		mainMenu.enabled = true;
		optionsMenu.enabled = false;
		videoMenu.enabled = false;
		soundMenu.enabled = false;
		loadMenu.enabled = false;
		SetFOVSlider();
		FOVSlider ();
		SetSFXSlider();
		SFXSlider();
		SetMusicSlider();
		MusicSlider();
	}

	void Update()
	{
		t = Mathf.PingPong(Time.time, duration) / duration;
		//Lerp1 ();
		Lerp2 ();
		//Lerp3 ();
	}

	public void Lerp1()
	{
		camera.backgroundColor = Color.Lerp(color1, color2, t);
	}

	public void Lerp2()
	{
		camera.backgroundColor = Color.Lerp(color2, color3, t);
	}

	public void Lerp3()
	{
		camera.backgroundColor = Color.Lerp(color3, color1, t);
	}
	
	public void LoadGame()
	{
		Application.LoadLevel (1);
	}
	
	public void KillGame()
	{
		#if UNITY_EDITOR 
		EditorApplication.isPlaying = false;
		#else 
		Application.Quit();
		#endif
	}

	public void OpenOptions()
	{
		mainMenu.enabled = false;
		optionsMenu.enabled = true;
	}

	public void OpenVideo()
	{
		optionsMenu.enabled = false;
		videoMenu.enabled = true;
	}

	public void OpenSound()
	{
		optionsMenu.enabled = false;
		soundMenu.enabled = true;
	}

	public void BackVideo()
	{
		videoMenu.enabled = false;
		optionsMenu.enabled = true;
	}

	public void BackSound()
	{
		soundMenu.enabled = false;
		optionsMenu.enabled = true;
	}

	public void CloseMenu()
	{
		optionsMenu.enabled = false;
		mainMenu.enabled = true;
	}

	public void SetDefPref()
	{
		if (!PlayerPrefs.HasKey ("FOV"))
		{
			fovValue = 70;
			PlayerPrefs.SetInt ("FOV", fovValue);
			PlayerPrefs.Save ();
		}

		if (!PlayerPrefs.HasKey("SFXVOL"))
		{
			sfxValue = 1f;
			musicValue = 1f;
			PlayerPrefs.SetFloat("SFXVOL", sfxValue);
			PlayerPrefs.SetFloat("MUSICVOL", musicValue);
			PlayerPrefs.Save();
		}
	}

	public void RestoreDefPrefVideo()
	{
		fovValue = 60;
		PlayerPrefs.SetInt ("FOV", fovValue);
		SetFOVSlider();
		FOVSlider();
		PlayerPrefs.Save ();

	}

	public void RestoreDefPrefAudio()
	{
		sfxValue = 1f;
		musicValue = 1f;
		PlayerPrefs.SetFloat("SFXVOL", sfxValue);
		PlayerPrefs.SetFloat("MUSICVOL", musicValue);
		SFXSlider();
		SetSFXSlider();
		MusicSlider();
		SetMusicSlider();
		PlayerPrefs.Save();
	}

	public void SavePrefVideo()
	{
		fovValue = (int)fovSlider.value;
		PlayerPrefs.SetInt ("FOV", fovValue);
		PlayerPrefs.Save ();
	}

	public void SavePrefAudio()
	{
		sfxValue = sfxSlider.value;
		musicValue = musicSlider.value;
		PlayerPrefs.SetFloat("SFXVOL", sfxValue);
		PlayerPrefs.SetFloat("MUSICVOL", musicValue);
		PlayerPrefs.Save();
	}

	public void FOVSlider()
	{
		fovText.text = "FOV: " + fovSlider.value.ToString();
	}

	public void SetFOVSlider()
	{
		fovSlider.value = PlayerPrefs.GetInt ("FOV");
	}

	public void SFXSlider()
	{
		sfxText.text = "Sound Effects Volume: " + sfxSlider.value.ToString();
	}

	public void SetSFXSlider()
	{
		sfxSlider.value = PlayerPrefs.GetFloat("SFXVOL");
	}

	public void MusicSlider()
	{
		musicText.text = "Music Volume: " + musicSlider.value.ToString();
	}

	public void SetMusicSlider()
	{
		musicSlider.value = PlayerPrefs.GetFloat("MUSICVOL");
	}

	public void MuteAll()
	{
		sfxValue = 0f;
		musicValue = 0f;
		PlayerPrefs.SetFloat("SFXVOL", sfxValue);
		PlayerPrefs.SetFloat("MUSICVOL", musicValue);
		SFXSlider();
		SetSFXSlider();
		MusicSlider();
		SetMusicSlider();
		PlayerPrefs.Save();
	}

	public void OpenLoadMenu()
	{
		mainMenu.enabled = false;
		loadMenu.enabled = true;
	}
	
	public void BackLoadMenu()
	{
		loadMenu.enabled = false;
		mainMenu.enabled = true;
	}
}